<template>
	<view>
		<view class="mainpadding2">
			<view class="mainpadding_top ffffff radius">
				<view class="titletext bold hei_text">评价</view>
				<textarea class="textareaclass margin_top main_size hei_text bold" placeholder="请输入评价内容~" v-model="content"></textarea>
			</view>
			<view class="mainpadding_top ffffff radius">
				<view class="titletext hei_text bold">上传图片</view>
				<view class="flexleft">
					<view class="itemimgBox flexcenter margin_top" v-for="(item,index) in images_text" :key="index">
						<view class="listleftimg">
							<image class="listleftimg radius"
								:src="item"
								mode="">
							</image>
							<view class="deleteimg" @click="deleteindex(index,images,images_text)"></view>
						</view>
					</view>
					<view class="itemimgBox flexcenter margin_top" v-if="images.length<4" @click="uploadImg(4,images,images_text)">
						<image class="listleftimg radius" src="@/static/userOrder/upimg.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="posibottom mainpadding">
				<view class="submitbtn" @click="submit">发表评价</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/commonFun/httpRequest.js");
	export default {
		data() {
			return {
				images:[],
				images_text:[],
				listid: "",
				content:"",
			}
		},
		onLoad(options) {
			this.listid = options.id
		},
		methods: {
			submit(){
				if(this.content==""){
					httpRequest.toast("请输入评论内容")
					return false
				}
				if(!this.images.length){
					httpRequest.toast("请上传图片")
					return false
				}
				httpRequest.request('/customapi/goods/goodsComment', 'GET', {
					goods_id: this.listid,
					images: this.images.join(),
					content: this.content
				}).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
			},
			deleteindex(index,upImgs, upImgs_text){
				upImgs.splice(index,1)
				upImgs_text.splice(index,1)
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
		}
	}
</script>

<style>

</style>